package com.fulu.game.bi.service.impl.market;

import com.fulu.game.bi.common.util.csv.CsvExportUtil;
import com.fulu.game.bi.common.util.csv.Mapper;
import com.fulu.game.bi.common.util.csv.Querier;
import com.fulu.game.bi.entity.enums.QueryTypeStatusEnum;
import com.fulu.game.bi.entity.po.market.UserRepurchaseStat;
import com.fulu.game.bi.entity.vo.market.CommonlyQueryVO;
import com.fulu.game.bi.mysql.bi.market.UserRepurchaseStatDao;
import com.fulu.game.bi.redis.service.market.UserRepurchaseService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserRepurchaseServiceImpl implements UserRepurchaseService {

    @Autowired
    private UserRepurchaseStatDao userRepurchaseStatDao;

    @Autowired
    private CommonlyStatService commonlyStatService;

    public static final Map<Integer, String> tableNameMap = new HashMap();

    static {
        tableNameMap.put(QueryTypeStatusEnum.DAY.getType(), "t_user_repurchase_stat_day");
        tableNameMap.put(QueryTypeStatusEnum.WEEK.getType(), "t_user_repurchase_stat_week");
        tableNameMap.put(QueryTypeStatusEnum.MONTH.getType(), "t_user_repurchase_stat_month");
    }

    @Override
    public List<UserRepurchaseStat> list(CommonlyQueryVO vo) {
        return commonlyStatService.list(vo, tableNameMap, () -> userRepurchaseStatDao.list(vo));
    }

    @Override
    public void export(CommonlyQueryVO vo) {
        Map<Integer, String> statTypeMap = new HashMap<>();
        statTypeMap.put(1, "复购次数");
        statTypeMap.put(2, "复购天数");
        String fileName = "用户复购" + statTypeMap.get(vo.getStatType()) + vo.getStatTimeStart() + "-" + vo.getStatTimeEnd();
        Map<Integer, String> repurchaseTypeMap = new HashMap<>();
        repurchaseTypeMap.put(1, "次");
        repurchaseTypeMap.put(2, "天");
        final String site = repurchaseTypeMap.get(vo.getStatType());
        LinkedHashMap<String, Mapper<UserRepurchaseStat>> map = new LinkedHashMap<>();
        map.put("日期", v -> v.getBeginStatDay() + "-" + v.getEndStatDay());
        map.put("渠道分组", v -> v.getSourceGroupName());
        map.put("渠道", v -> v.getSourceName());
        map.put("标签分组", v -> v.getLabelGroupName());
        map.put("标签", v -> v.getLabelName());
        map.put("系统", v -> v.getRegisterTypeName());
        map.put("付费用户数", v -> v.getUserNum() + "");

        map.put("1" + site + "用户付费人数", v -> v.getTotalNum1() + "");
        map.put("1" + site + "用户人数占比", v -> v.getTotalRatio1() + "");
        map.put("2" + site + "用户付费人数", v -> v.getTotalNum2() + "");
        map.put("2" + site + "用户人数占比", v -> v.getTotalRatio2() + "");
        map.put("3" + site + "及以上用户付费人数", v -> v.getTotalNum3() + "");
        map.put("3" + site + "及以上用户人数占比", v -> v.getTotalRatio3() + "");

        map.put("1" + site + "新用户付费人数", v -> v.getNewNum1() + "");
        map.put("1" + site + "新用户人数占比", v -> v.getNewRatio1() + "");
        map.put("2" + site + "新用户付费人数", v -> v.getNewNum2() + "");
        map.put("2" + site + "新用户人数占比", v -> v.getNewRatio2() + "");
        map.put("3" + site + "及以上新用户付费人数", v -> v.getNewNum3() + "");
        map.put("3" + site + "及以上新用户人数占比", v -> v.getNewRatio3() + "");

        map.put("1" + site + "老用户付费人数", v -> v.getOldNum1() + "");
        map.put("1" + site + "老用户人数占比", v -> v.getOldRatio1() + "");
        map.put("2" + site + "老用户付费人数", v -> v.getOldNum2() + "");
        map.put("2" + site + "老用户人数占比", v -> v.getOldRatio2() + "");
        map.put("3" + site + "及以上老用户付费人数", v -> v.getOldNum3() + "");
        map.put("3" + site + "及以上老用户人数占比", v -> v.getOldRatio3() + "");
        Querier querier = (num) -> {
            List<UserRepurchaseStat> list = list(vo);
            PageInfo pageInfo = new PageInfo(list);
            pageInfo.setHasNextPage(false);
            return pageInfo;
        };

        CsvExportUtil.export(100, fileName, map, querier);
    }
}
